﻿using System;
using System.Web.UI.WebControls;
using System.Data;
using DMS.Business.Utility;
using DMS.Business.WebControls;

namespace DMS.Web.Common.Controls
{
    public partial class WorkflowAttachments : System.Web.UI.UserControl
    {

        public string InstanceId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
                LoadAttachments();
            //}
        }

        public string FileServer
        {
            get
            {
                return AppParams.FileServer;
            }
        }

        private bool _canDelete = true;
        public bool CanDelete
        {
            get
            {
                return _canDelete;
            }
            set
            {
                _canDelete = value;
            }
        }

        private void LoadAttachments()
        {

            //流程中的附件
            string instanceID = Request.QueryString["instanceID"];

            if(string.IsNullOrEmpty(instanceID))
            {
                instanceID = this.InstanceId;
            }

            if (string.IsNullOrEmpty(instanceID))
            {
                instanceID = Guid.NewGuid().ToString(); ;
            }
            DataSet ds = WorkFlowAttachmentHelper.GetAttachmentByInstanceID(instanceID);
            if (ds.Tables[0].Rows.Count == 0)
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                GridView1.DataSource = ds.Tables[0].DefaultView;
                GridView1.DataSource = ds;
                GridView1.DataBind();
                GridView1.Rows[0].Cells.Clear();
            }
            else
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            TableCell cell = GridView1.Rows[e.RowIndex].Cells[0];

            FileLink link = cell.FindControl("FileLink1") as FileLink;

            WorkFlowAttachmentHelper.DeleteAttachment(link.FileUrl);

            LoadAttachments();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dr = e.Row.DataItem as DataRowView;

                FileLink link = e.Row.FindControl("FileLink1") as FileLink;
                link.Text = dr["AttachName"].ToString();
                link.FileUrl = dr["AttachURL"].ToString();
                link.FileName = dr["AttachName"].ToString();


                LinkButton lbtnDelete = e.Row.FindControl("lbtnDelete") as LinkButton;
                lbtnDelete.Visible = CanDelete;
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }
    }
}